using System;
using Gtk;

using System.Data;

using MySql.Data;
using MySql.Data.MySqlClient;

public partial class MainWindow : Gtk.Window
{
	MySqlConnection connection = new MySqlConnection();
	
	
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{Build ();}
	
	protected virtual void OnConectarActionActivated (object sender, System.EventArgs e)
	{connection = AbrirConector();}
	protected virtual void OnNuevaConexionActivated (object sender, System.EventArgs e)
	{connection= AbrirConector();}
	
	
	protected virtual void OnLibroActionActivated (object sender, System.EventArgs e)
	{Consulta("select * from libro;");}
	protected virtual void OnAutorActionActivated (object sender, System.EventArgs e)
	{Consulta("select * from autor;");}
	protected virtual void OnEditorialActionActivated (object sender, System.EventArgs e)
	{Consulta("select * from editorial;");}
	protected virtual void OnBuscarActivated (object sender, System.EventArgs e)
	{entry.Text = "select * from libro, autor, editorial;";}
	protected virtual void OnCerrarConexionActivated (object sender, System.EventArgs e)
	{CerrarConector(connection);}
	protected virtual void OnButtonClicked (object sender, System.EventArgs e)
	{
		string cons;
		cons = entry.Text;
		Consulta(cons);
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{Salir();}
	
/*-------------------------AQUI LOS METODOS----------------------------------*/
	
	//Establece la conexion con el servidor
	public MySqlConnection AbrirConector(){
		MySqlConnection connection = new MySqlConnection(
		                                 "Server=localhost;" +
		                                 "Database=libro;" +
		                                 "Uid=root;" +
		                                 "Pwd=sistemas"
		                             );
		
		
		connection.Open();
		label2.Text = "Conexion realizada con exito";
		return connection;
	}
	
	public void vaciarTreeView (){
		TreeViewColumn[] columnas = treeview.Columns;
		foreach(TreeViewColumn columna in columnas)
			treeview.RemoveColumn(columna);
	}
	
	
	//Realiza una consulta con lo que quieras visualizar
	public void Consulta(string consulta){
		MySqlCommand command = connection.CreateCommand();
		command.CommandText = consulta;
		
		MySqlDataReader dataReader = command.ExecuteReader();
		
		TreeViewColumn[] columnas = treeview.Columns;
		foreach(TreeViewColumn columna in columnas)
			treeview.RemoveColumn(columna);
				
		for(int index = 0; index < dataReader.FieldCount; index++){
			treeview.AppendColumn(dataReader.GetName(index),new CellRendererText(),"text", index);
		}
		
		int count = dataReader.FieldCount;
		Type[] types = new Type[count];
		for(int i = 0; i < count;i++){
			types[i] = typeof(string);	
		}
		
	 	ListStore listStore = new ListStore(types);
		
		while(dataReader.Read()){	
			string[] row = new string[count];
			for(int i = 0; i < count; i++){
				row[i] = dataReader[i].ToString();
			}
			listStore.AppendValues(row);
		}	
		
		treeview.Model = listStore;
		treeview.ShowAll();
		dataReader.Close();
	
	}
	
	//Cierra la conexion con el servidor
	public void CerrarConector(MySqlConnection connection){
		label2.Text = "Desconexión realizada con éxito";
		connection.Close();
	}
	
	//Cierra el programa
	public void Salir(){
		CerrarConector(connection);
		Application.Quit ();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

